package com.zzyl.nursing.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.nursing.domain.pojo.Bed;
import com.zzyl.nursing.domain.pojo.Floor;
import com.zzyl.nursing.domain.pojo.Room;
import com.zzyl.nursing.domain.vo.FloorVo;
import com.zzyl.nursing.domain.vo.TreeVo;
import com.zzyl.nursing.mapper.BedMapper;
import com.zzyl.nursing.mapper.FloorMapper;
import com.zzyl.nursing.mapper.RoomMapper;
import com.zzyl.nursing.service.IFloorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 楼层Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-26
 */
@Service
public class FloorServiceImpl extends ServiceImpl<FloorMapper, Floor> implements IFloorService
{
    @Autowired
    private FloorMapper floorMapper;

    /**
     * 查询楼层
     *
     * @param id 楼层主键
     * @return 楼层
     */
    @Override
    public Floor selectFloorById(Long id)
    {
        return getById(id);
    }

    /**
     * 新增楼层
     *
     * @param floor 楼层
     * @return 结果
     */
    @Override
    public int insertFloor(Floor floor)
    {
        return save(floor) ? 1 : 0;
    }

    /**
     * 修改楼层
     *
     * @param floor 楼层
     * @return 结果
     */
    @Override
    public int updateFloor(Floor floor)
    {
        return updateById(floor) ? 1 : 0;
    }

    /**
     * 批量删除楼层
     *
     * @param ids 需要删除的楼层主键
     * @return 结果
     */
    @Override
    public int deleteFloorByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除楼层信息
     *
     * @param id 楼层主键
     * @return 结果
     */
    @Override
    public int deleteFloorById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 查询所有楼层（负责老人）
     */
    @Override
    public List<Floor> selectAllByNur() {
        return floorMapper.selectAllByNur();
    }

    @Autowired
    BedMapper bedMapper;
    @Autowired
    RoomMapper roomMapper;
    @Override
    public List<TreeVo> getRoomAndBedByBedStatus(Integer status) {
//        List<TreeVo> treeVos= floorMapper.getRoomAndBedByBedStatus(status);
        List<Floor> floorList = this.list();
        Set<Long> floorIds = floorList.stream().map(floor -> floor.getId()).collect(Collectors.toSet());
        List<Room> roomList = roomMapper.selectList(Wrappers.<Room>lambdaQuery().in(Room::getFloorId, floorIds));
        Set<Long> roomIds = roomList.stream().map(room -> room.getId()).collect(Collectors.toSet());
        List<Bed> bedList = bedMapper.selectList(Wrappers.<Bed>lambdaQuery().in(Bed::getRoomId, roomIds));
        Map<Long, List<TreeVo>> bedMap = bedList.stream().collect(Collectors.groupingBy(Bed::getRoomId, Collectors.mapping(bed -> TreeVo.builder().value(String.valueOf(bed.getId())).label(bed.getBedNumber()).build(), Collectors.toList())));
        Map<Long, List<TreeVo>> roomMap = roomList.stream().collect(Collectors.groupingBy(Room::getFloorId, Collectors.mapping(room -> TreeVo.builder().value(String.valueOf(room.getId())).label(room.getCode()).children(bedMap.getOrDefault(room.getId(), Collections.emptyList())).build(), Collectors.toList())));
        List<TreeVo> t = floorList.stream().collect(Collectors.mapping(floor -> TreeVo.builder().value(String.valueOf(floor.getId())).label(floor.getName()).children(roomMap.getOrDefault(floor.getId(), Collections.emptyList())).build(), Collectors.toList()));

        return t;

    }

    @Override
    public List<FloorVo> getAllFloorsWithDevice() {

        return floorMapper.getAllFloorsWithDevice();
    }

}